package com.sentiment.algorithm;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;

public class KmpCoreNo {
	private String patternFileName;
	// private String[] artics;
	private ArrayList<RecordBean> recordBeans = new ArrayList<RecordBean>();

	public KmpCoreNo() {

	}

	// public String[] getArtics() {
	// return artics;
	// }
	//
	// public void setArtics(String[] artics) {
	// this.artics = artics;
	// }

	public String getPatternFileName() {
		return patternFileName;
	}

	public void setPatternFileName(String patternFileName) {
		this.patternFileName = patternFileName;
	}

	public void showSimi() {
		for (RecordBean recordBean : recordBeans) {
			System.out.println(recordBean.toFile());
		}
	}

	public void writeToFileSimi(String filePath) {
		try {
			PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter(filePath)));
			for (RecordBean recordBean : recordBeans) {
				output.println(recordBean.toPattern());
			}
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String writeToDBSimi() {
		StringBuilder strb = new StringBuilder();
		for (RecordBean recordBean : recordBeans) {
			strb.append(recordBean.toPattern() + "\n");
		}
		return strb.toString();

	}

	// public static void main(String[] args) {
	// KmpCore mini = new KmpCore();
	// LoadConfigurationXMLFile loadConfigurationXMLFile = new
	// LoadConfigurationXMLFile(args[0]);
	// loadConfigurationXMLFile.loadConfigurationXML();
	// mini.setPatternFileName(ArgumentConf.PATTERNPATH);
	// SelfFile selfFile = new SelfFile();
	//
	// mini.setArtics(selfFile.ShowDirListPath(ArgumentConf.articlesFilePath));
	// mini.kmpJob();
	// mini.showSimi();
	// mini.writeToFileSimi(ArgumentConf.similaroutputPath);
	//
	// }

	/*
	 * 
	 */
	public void kmpJob(String pattext, String arttext, int art, String artname) {

		String[] texts = pattext.split("\n");
		int patt = 0;
		while (patt < texts.length) {
			//不需要替换空格　在存入数据库时已经做过处理
//			String regex = "^[\\u0020\\u3000]*$";
			String pattern_text = texts[patt];

			if (pattern_text.trim().isEmpty()) {
				patt++;
				continue;
			}
			// int art=0;
			KmpJobSin(art, pattern_text, arttext, patt, artname);
			// while(art<artics.length){
			//
			// art++;
			// }
			patt++;
		}
	}

	/*
	 * pattern_text:输入的待检测文档一段话 articletext：输入的一篇爬取下来的文章 本函数检测一段话在一篇文章的相似性
	 */
	public void KmpJobSin(int art, String pattern_text, String articletext, int patt, String artname) {

//		String regex = "^[\\u0020\\u3000]*$";

		String[] texts = articletext.split("\n");
		int tag = 0;
		while (tag < texts.length) {
			String article_text_in = texts[tag];
			if (article_text_in.trim().isEmpty()) {
				tag++;
				continue;
			}
			KmpMethod kmpMethod = new KmpMethod(pattern_text, patt, article_text_in, tag);
			kmpMethod.selfPattern();
			ArrayList<KmpMethod.Recoed> arrayList = kmpMethod.getRecoeds();
			for (KmpMethod.Recoed recoed : arrayList) {

				RecordBean recordBean = new RecordBean(patt, recoed.getPoint(), artname, tag, recoed.getTag(),
						recoed.getSimilar());
				recordBeans.add(recordBean);
			}
			tag++;
		}

	}
}
